openerp.msap_masonry = function(instance, local) {
    var _t = instance.web._t,
        _lt = instance.web._lt;
    var QWeb = instance.web.qweb;

    instance.web_kanban.KanbanView.include({
        _masonry: function() {
            $('.grid').masonry('reloadItems').masonry({
                itemSelector: '.grid-item',
                gutter: 5,
                isAnimated: true,
            })
        },
        do_masonry: function() {
            var self = this;
            if ($('.grid img')) {
                var count_per_row = Math.floor(self.$el.width() / $('.grid .grid-item:first').width());
                $.each($('.grid img'), function(index, el) {
                    if ((index + 1) % count_per_row == 0 || index == $('.grid img').length - 1) {
                        $(el).off('load').on('load', function() {
                            self._masonry();
                        });
                    }
                });
            }
            $('.grid').ready(function() {
                self._masonry();
            });
        },
        do_search: function(domain, context, group_by) {
            var self = this;
            return this._super(domain, context, group_by).then(function(res) {
                self.do_masonry();
            });
        },
        postprocess_m2m_tags: function() {
            var self = this;
            if (!this.many2manys.length) {
                self.do_masonry();
                return;
            }
            var relations = {};
            this.groups.forEach(function(group) {
                group.records.forEach(function(record) {
                    self.many2manys.forEach(function(name) {
                        var field = record.record[name];
                        var $el = record.$('.oe_form_field.oe_tags[name=' + name + ']').empty();
                        if (!relations[field.relation]) {
                            relations[field.relation] = {
                                ids: [],
                                elements: {},
                                context: self.m2m_context[name]
                            };
                        }
                        var rel = relations[field.relation];
                        field.raw_value.forEach(function(id) {
                            rel.ids.push(id);
                            if (!rel.elements[id]) {
                                rel.elements[id] = [];
                            }
                            rel.elements[id].push($el[0]);
                        });
                    });
                });
            });
            _.each(relations, function(rel, rel_name) {
                var dataset = new instance.web.DataSetSearch(self, rel_name, self.dataset.get_context(rel.context));
                dataset.name_get(_.uniq(rel.ids)).done(function(result) {
                    result.forEach(function(nameget) {
                        $(rel.elements[nameget[0]]).append('<span class="oe_tag">' + _.str.escapeHTML(nameget[1]) + '</span>');
                    });
                    self.do_masonry();
                });
            });
        }
    });


}
